/* eslint-disable */

window.WPT = window.WPT || {};
window.WPT.Model = window.WPT.Model || {};

var successPay = function () {};
var cancelPay = function () {};
var failPay = function () {};
function mySuccessPay(o) {
    successPay(o);
}
function myCancelPay(o) {
    cancelPay(o);
}
function myFailPay(o) {
    cancelPay(o);
}

var successCode = function () {};
function mySuccessCode(code) {
    var res = new Object();
    res.errMsg = "scanQRCode:ok";
    res.resultStr = "," + code;
    successCode(res);
}

var successAddress = function () {};
function mySuccessAddress(o) {
    var strs = new Array();
    strs = o.split(",");

    var res = new Object();
    res.errMsg = strs[0];
    res.userName = strs[1];
    res.postalCode = strs[2];
    res.provinceName = strs[3];
    res.cityName = strs[4];
    res.countryName = strs[5];
    res.detailInfo = strs[6];
    res.nationalCode = strs[7];
    res.telNumber = strs[8];
    successAddress(res);
}

var successStopRecord = function () {};
function mySuccessStopRecord(localId) {
    var res = new Object();
    res.localId = localId;
    successStopRecord(res);
}

var successOnVoicePlayEnd = function () {};
function mySuccessOnVoicePlayEnd(localId) {
    var res = new Object();
    res.localId = localId;
    successOnVoicePlayEnd(res);
}

var successUploadVoice = function () {};
function mySuccessUploadVoice(serverId) {
    var res = new Object();
    res.serverId = serverId;
    successUploadVoice(res);
}

var successDownloadVoice = function () {};
function mySuccessDownloadVoice(serverId) {
    var res = new Object();
    res.localId = serverId;
    successDownloadVoice(res);
}

var failDownloadVoice = function () {};
function myFailDownloadVoice() {
    var res = new Object();
    failDownloadVoice(res);
}

var successChooseImage = function () {};
function mySuccessChooseImage(localIds) {
    var res = new Object();
    res.localIds = eval(localIds);
    successChooseImage(res);
}

var successUploadImage = function () {};
var failUploadImage = function () {};
function mySuccessUploadImage(serverId) {
    successUploadImage(serverId);
}
function myFailUploadImage(){
    failUploadImage();
}

var successChangeUser = function () {};
var cancelChangeUser = function () {};
var failChangeUser = function () {};
function mySuccessChangeUser() {
    var res = new Object();
    successChangeUser(res);
}
function myCancelChangeUser() {
    var res = new Object();
    cancelChangeUser(res);
}
function myFailChangeUser() {
    var res = new Object();
    failChangeUser(res);
}

var successShare = function () {};
var failShare = function () {};
var cancelShare = function () {};
function mySuccessShare(o) {
    successShare(o);
}
function myFailShare(o) {
    failShare(o);
}
function myCancelShare(o) {
    cancelShare(o);
}

var successLogin = function () {};
var failLogin = function () {};
function mySuccessLogin(o) {
     if(successLogin){
        successLogin();
    } else {
        var options = [];
        WPT.loginRefreshUserinfo(function(res) {
            WPT.userinfo.isSubscribe = 1;
        }, options);
    }
}
function myFailLogin(o) {
    failLogin && failLogin();
}

var successVideo = function () {};
var progressVideo = function () {};
var failVideo = function () {};
function mySuccessVideo(key){
   successVideo(key);
}
function myProgressVideo(progress){
   progressVideo(progress);
}
function myFailVideo(fail){
   failVideo(fail);
}

var installBank = function () {};
function appInstalledBank(bankArr) {
    installBank(bankArr)
}

var wx = {
    openAddress : function (o) {
        successAddress = o.success;
        window.webkit.messageHandlers.openAddress.postMessage("1");
    },
    scanQRCode : function (o) {
        successCode = o.success;
        window.webkit.messageHandlers.wptScanQRCode.postMessage("1");
    },
    previewImage : function (o) {
        window.webkit.messageHandlers.wptPreviewImage.postMessage(o);
    },
    startRecord : function () {
        window.webkit.messageHandlers.wptStartRecord.postMessage("1");
    },
    stopRecord : function (o) {
        successStopRecord = o.success;
        window.webkit.messageHandlers.wptStopRecord.postMessage("1");
    },
    downloadVoice : function (o) {
        successDownloadVoice = o.success;
        failDownloadVoice = o.fail;
        var newObj = new Object();
        newObj.serverId = o.serverId;
        newObj.isShowProgressTips = o.isShowProgressTips;
        window.webkit.messageHandlers.wptDownloadVoice.postMessage(newObj);
    },
    onVoiceRecordEnd : function (o) {
        successStopRecord = o.complete;
        window.webkit.messageHandlers.wptOnVoiceRecordEnd.postMessage("1");
    },
    playVoice : function (o) {
        window.webkit.messageHandlers.wptPlayVoice.postMessage(o.localId);
    },
    pauseVoice : function (o) {
        window.webkit.messageHandlers.wptPauseVoice.postMessage(o.localId);
    },
    pauseVoice : function (o) {
        window.webkit.messageHandlers.wptPauseVoice.postMessage(o.localId);
    },
    pauseVoice : function (o) {
        window.webkit.messageHandlers.wptPauseVoice.postMessage(o.localId);
    },
    stopVoice : function (o) {
        window.webkit.messageHandlers.wptStopVoice.postMessage(o.localId);
    },
    onVoicePlayEnd : function (o) {
        successOnVoicePlayEnd = o.success;
        window.webkit.messageHandlers.wptOnVoicePlayEnd.postMessage("1");
    },
    uploadVoice : function (o) {
        successUploadVoice = o.success;
        window.webkit.messageHandlers.wptUploadVoice.postMessage(o.isShowProgressTips);
    },
    chooseImage : function(o){
        successChooseImage = o.success;
        window.webkit.messageHandlers.wptChooseImage.postMessage(o.count);
    },
    uploadImage : function (o) {
        successUploadImage = o.success;
        failUploadImage = o.fail;
        var newObj = new Object();
        newObj.localId = o.localId;
        newObj.isShowProgressTips = o.isShowProgressTips;
        window.webkit.messageHandlers.wptUploadImage.postMessage(newObj);
    },
    chooseWXPay : function (o) {
        successPay = o.success;
        cancelPay = o.cancel;
        var newObj = new Object();
        newObj.timestamp = o.timestamp;
        newObj.nonceStr = o.nonceStr;
        newObj.package = o.package;
        newObj.paySign = o.paySign;
        window.webkit.messageHandlers.wptChooseWXPay.postMessage(newObj);
    },
    downloadImage: function(o) {
        window.webkit.messageHandlers.wptDownloadImage.postMessage(o.serverId);
    },
    checkBankInstall: function(o) {
        installBank = o.success;
        window.webkit.messageHandlers.wptCheckBankInstall.postMessage(o.key);
    }
};

var app = {
    wptAppChooseWXPay: function (o) {
        successPay = o.success;
        cancelPay = o.cancel;
        failPay = o.fail;
        var newObj = new Object();
        newObj.timestamp = o.timestamp;
        newObj.noncestr = o.noncestr;
        newObj.package = o.package;
        newObj.paysign = o.paysign;
        newObj.appid = o.appid;
        newObj.prepayid = o.prepayid;
        newObj.partnerid = o.partnerid;
        window.webkit.messageHandlers.wptAppChooseWXPay.postMessage(newObj);
    },
    wptAppChangeUser: function (o) {
        successChangeUser = o.success;
        cancelChangeUser = o.cancel;
        failChangeUser = o.fail;
        var newObj = new Object();
        newObj.id = o.userInfoId;
        newObj.openid = o.openid;
        newObj.type = o.type;
        window.webkit.messageHandlers.wptAppChangeUser.postMessage(newObj);
    },
     wptAppChooseAliPay: function(o){
        successPay = o.success;
        cancelPay = o.cancel;
        failPay = o.fail;
        window.webkit.messageHandlers.wptAppChooseAliPay.postMessage(o.chooseAliPayParam);
    },
    wptAppChooseQPay: function(o){

        successPay = o.success;
        failPay = o.fail;
        cancelPay = o.cancel;
        var newObj = new Object();
        newObj = o.payParam;
        window.webkit.messageHandlers.wptAPPChooseQQWalletPay.postMessage(newObj);
        
    },

};

WPT.showLogin = function(cb, fcb) {
    successLogin = cb;
    failLogin = fcb;
    window.webkit.messageHandlers.wptLogin.postMessage("0");
};


var successLogout = function () {};
function mySuccessLogout(o) {  
    if (successLogout) {
        successLogout();
    } else {
        if ($.os.ios) {
            wptAppSetCookie('userinfo_cookie', '', -1);
        }
        wptAppSetCookie('wptLogoutAction', '/my', 1);
    }
}
WPT.logout = function(o){
    successLogout = o.success;
    window.webkit.messageHandlers.wptLoginOut.postMessage("0");
}
WPT.uploadVideo = function(o){
   successVideo = o.onSuccess;
   progressVideo = o.onProgress;
   failVideo = o.onFail;
   var newObj = new Object();
    newObj.videoLimit = o.videoLimit;
    newObj.bType = o.bType;
    window.webkit.messageHandlers.wptQiniuVideoUploader.postMessage(newObj);
};

WPT.uploadImg = function(o){
    successUploadImage = o.success;
    window.webkit.messageHandlers.wptAppUploadImagers.postMessage({
      "count": o.count,
      "isQr": o.isQr || false
    });
};

function wptShare(flag) {
    var newObj = new Object();
    newObj.title = wxShare.title;
    newObj.desc = wxShare.desc;
    newObj.link = wxShare.link;
    newObj.imgUrl = wxShare.imgUrl;
    newObj.timeline = wxShare.timeline;
    newObj.summary = wxShare.summary;
    newObj.needWechatQr = wxShare.needWechatQr;
    newObj.pics = wxShare.pics;
    newObj.qLink = wxShare.qLink;
    newObj.cLink = wxShare.cLink;
    newObj.qDesc = wxShare.qDesc;
    newObj.wbDesc = wxShare.wbDesc;
    newObj.video = wxShare.video;
    newObj.uri = wxShare.uri;
    newObj.uriQr = wxShare.uriQr;
    newObj.messageLink = wxShare.messageLink;
    newObj.timelineLink = wxShare.timelineLink;
    newObj.qqLink = wxShare.qqLink;
    newObj.qqZoneLink = wxShare.qqZoneLink;
    newObj.weiboLink = wxShare.weiboLink;
    newObj.shareType = wxShare.shareType;
    newObj.noShareArr = wxShare.noShareArr;
    newObj.activityUri = wxShare.activityUri;
    newObj.miniProgramPath = wxShare.miniProgramPath;
    newObj.miniProgramShareContent = wxShare.miniProgramShareContent;
    newObj.isShareMiniProgramCode = wxShare.isShareMiniProgramCode;
    if (flag instanceof Array) {
        newObj.noShareArr = flag;
    };
    successShare = wxShare.success;
    failShare = wxShare.fail;
    cancelShare = wxShare.cancel;
    window.webkit.messageHandlers.wptShare.postMessage(newObj);
}


//分享+ 有奖点击回调
var wptSharePlusClickAward = function () {};
function myWptSharePlusClickAward(){
   wptSharePlusClickAward();
}
//分享+
function wptSharePlus(o,callback,failCallBack,clickAward){
    successShare = callback;
    failShare = failCallBack;
    wptSharePlusClickAward = clickAward;
    window.webkit.messageHandlers.wptSharePlus.postMessage(o);
}
//图片分享
function wptImageShare(imgUrl,e){ 
    var newObj = new Object();
    newObj.imgUrl = imgUrl;
    if (e instanceof Array) {
        newObj.withoutKey = e;
    };
    window.webkit.messageHandlers.wptImageShare.postMessage(newObj);
}

var wptQRShareObj = null;
function wptQRShare(o){
    wptQRShareObj = o;
    window.webkit.messageHandlers.wptQRShare.postMessage(wptQRShareObj);
}

function getWptQRShareObj(){
    window.webkit.messageHandlers.wptGetqrShareShopImg.postMessage(wptQRShareObj.imgUrl);
}

var successWptAppShare = function () {};
var failWptAppShare = function () {};
var cancelWptAppShare = function () {};
function mySuccessWptAppShare(o) {  
    successWptAppShare();
}
function myFailWptAppShare(o){
    failWptAppShare();
}
function myCancelWptAppShare(o){
    cancelWptAppShare();
}
function wptAppShare(o){
    successWptAppShare = o.success;
    failWptAppShare = o.fail;
    cancelWptAppShare = o.cancel;
    var newObj = new Object();
    newObj = o.appShareData;
    window.webkit.messageHandlers.wptAppShare.postMessage(newObj);
}
function wptAboutView(){
    window.webkit.messageHandlers.wptAboutView.postMessage("0");
}

WPT.Model.share = function () {
    var newObj = new Object();
    newObj.title = wxShare.title;
    newObj.desc = wxShare.desc;
    newObj.link = wxShare.link;
    newObj.imgUrl = wxShare.imgUrl;
    newObj.needWechatQr = wxShare.needWechatQr;
    newObj.timeline = wxShare.timeline;
    newObj.summary = wxShare.summary;
    newObj.pics = wxShare.pics;
    newObj.video = wxShare.video;
    newObj.uri = wxShare.uri;
    newObj.uriQr = wxShare.uriQr;
    newObj.qLink = wxShare.qLink;
    newObj.cLink = wxShare.cLink;
    newObj.qDesc = wxShare.qDesc;
    newObj.wbDesc = wxShare.wbDesc;
    newObj.messageLink = wxShare.messageLink;
    newObj.timelineLink = wxShare.timelineLink;
    newObj.qqLink = wxShare.qqLink;
    newObj.qqZoneLink = wxShare.qqZoneLink;
    newObj.weiboLink = wxShare.weiboLink;
    newObj.shareType = wxShare.shareType;
    newObj.noShareArr = wxShare.noShareArr;
    newObj.activityUri = wxShare.activityUri;
    newObj.miniProgramPath = wxShare.miniProgramPath;
    newObj.miniProgramShareContent = wxShare.miniProgramShareContent;
    newObj.isShareMiniProgramCode = wxShare.isShareMiniProgramCode;
    successShare = wxShare.success;
    failShare = wxShare.fail;
    cancelShare = wxShare.cancel;
    window.webkit.messageHandlers.wptShare.postMessage(newObj);
}

function checkShare() {

    if (typeof(wxShare) == "object") {
        var linkTemp = wxShare.link;
        if (linkTemp !== null && linkTemp !== undefined && linkTemp !== '') {
            var newObj = new Object();
            newObj.title = wxShare.title;
            newObj.desc = wxShare.desc;
            newObj.link = wxShare.link;
            newObj.imgUrl = wxShare.imgUrl;
            newObj.timeline = wxShare.timeline;
            newObj.summary = wxShare.summary;
            newObj.pics = wxShare.pics;
            newObj.wbDesc = wxShare.wbDesc;
            newObj.qLink = wxShare.qLink;
            newObj.cLink = wxShare.cLink;
            newObj.qDesc = wxShare.qDesc;
            newObj.video = wxShare.video;
            newObj.uri = wxShare.uri;
            newObj.uriQr = wxShare.uriQr;
            newObj.messageLink = wxShare.messageLink;
            newObj.timelineLink = wxShare.timelineLink;
            newObj.qqLink = wxShare.qqLink;
            newObj.qqZoneLink = wxShare.qqZoneLink;
            newObj.weiboLink = wxShare.weiboLink;
            newObj.shareType = wxShare.shareType;
            newObj.noShareArr = wxShare.noShareArr;
            newObj.activityUri = wxShare.activityUri;
            newObj.miniProgramPath = wxShare.miniProgramPath;
            newObj.miniProgramShareContent = wxShare.miniProgramShareContent;
            newObj.isShareMiniProgramCode = wxShare.isShareMiniProgramCode;
            window.webkit.messageHandlers.wptCheckShare.postMessage(newObj);
        } else {
            window.webkit.messageHandlers.wptCheckShare.postMessage("0");
        }
    } else {
        window.webkit.messageHandlers.wptCheckShare.postMessage("0");
    }
}

function writeUserInfo(o){
    var strs = new Array();
    strs = o.split(",");

    var res = new Object();
    res.buyerLevel = strs[0];
    res.hasTelephone = strs[1];
    res.headimgurl = strs[2];
    res.isLogin = strs[3];
    res.isSubscribe = strs[4];
    res.sellLevel = strs[5];
    res.uri = strs[6];

    WPT.userinfo.uri = res.data.uri;
    WPT.userinfo.headimgurl = res.headimgurl;
    // 下面 5项目window 下也需要放一份
    WPT.userinfo.isSubscribe = res.isSubscribe;
    WPT.userinfo.sellLevel = res.sellLevel;
    WPT.userinfo.buyerLevel = res.buyerLevel;
    WPT.userinfo.isLogin = res.isLogin;
    WPT.userinfo.hasTelephone = res.telephone ? 11111111111 : 0;
    window.isSubscribe = res.isSubscribe;
    window.sellLevel = res.sellLevel;
    window.buyerLevel = res.buyerLevel;
    window.isLogin = res.isLogin;
    window.hasTelephone = res.telephone ? 11111111111 : 0;
}

var respWptJumpNativeWithUrl = function () {};
function myRespWptJumpNativeWithUrl(o) {  
    respWptJumpNativeWithUrl(o);
}
//Web跳转到原生页面
function wptJumpNativeWithUrl(url,cb,o){ //url
    respWptJumpNativeWithUrl = cb;
    var newObj = new Object();
    newObj.url = url;
    newObj.param = o;

    window.webkit.messageHandlers.wptJumpNativeWithUrl.postMessage(newObj);
}


var childAccountLoginSuccess = function () {};
var childAccountLoginFail = function () {};
function myChildAccountLoginSuccess(o){
    childAccountLoginSuccess(o);
}
function myChildAccountLoginFail(){
    childAccountLoginFail();
}
//子账号登录
function wptChildAccountLogin(o,callback,failCallBack){
    childAccountLoginSuccess = callback;
    childAccountLoginFail = failCallBack;
    window.webkit.messageHandlers.wptChildAccountLogin.postMessage(o);
}
var childAccountLogoutSuccess = function () {};
function myChildAccountLogoutSuccess(o){
    childAccountLogoutSuccess(o);
}
//子账号登出
function wptChildAccountLogout(o,callback){
    childAccountLogoutSuccess = callback;
    window.webkit.messageHandlers.wptChildAccountLogout.postMessage(o);
}

//触发h5订阅的事件
function triggerAppEvent(eventName, params){
    $(document).trigger('app-event-' + eventName, params);  
}

function wptAppSetCookie(name, value, expiredays, domain) {
    var exdate = {};
    var domain = domain || '.weipaitang.com';
    if (typeof expiredays === 'number') {
        exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
    } else if (typeof expiredays === 'object') {
        exdate = expiredays;
    }
    var expireStr = expiredays == null ? ';domain=' + domain : ';domain=' + domain + ';expires=' + exdate.toGMTString();
    document.cookie = name + '=' + encodeURIComponent(value) + expireStr + ';path=/';
}


$(document).trigger('wptLoadJs:success', {});    

$(document).on('Page:change', function(e,url){
    // window.webkit.messageHandlers.wptPageChage.postMessage(url);
})




